﻿using CL.Game.BLL;
using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebAPI.WFTPay
{
    public partial class Notify_wechat : System.Web.UI.Page
    {
        private Log log = new Log("Notify_wechat");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.RequestType == "POST")
            {
                CallBack();
            }
        }
        private void CallBack()
        {
            WeChatPayHelper helper = new WeChatPayHelper();
            using (StreamReader sr = new StreamReader(Request.InputStream))
            {
                try
                {
                    string ReadEnd = sr.ReadToEnd().Trim().Replace(" ", "");
                    log.Write("微信服务端回调数据：" + ReadEnd, true);
                    helper.setContent(ReadEnd);
                    SortedDictionary<string, string> resParam = helper.GetAllParameters();
                    log.Write("微信支付服务端加密验证：" + helper.isTenpaySign(), true);
                    if (helper.isTenpaySign())
                    {
                        //检查支付结果中transaction_id是否存在
                        if (!resParam.ContainsKey("transaction_id"))
                        {
                            //若transaction_id不存在，则立即返回结果给微信支付后台
                            WxPayData res = new WxPayData();
                            res.SetValue("return_code", "FAIL");
                            res.SetValue("return_msg", "支付结果中微信订单号不存在");
                            log.Write("支付结果中微信订单号不存在", true);
                            Response.Write(res.ToXml());
                            Response.End();
                        }
                        if (resParam["result_code"].ToString().ToUpper().Equals("SUCCESS"))
                        {
                            bool ResultHelper = false;
                            string out_trade_no = resParam["out_trade_no"].ToString();
                            string transaction_id = resParam["transaction_id"].ToString();
                            long total_fee = Convert.ToInt64(resParam["total_fee"]);
                            ResultHelper = new UsersRecordBLL().PayInfo(out_trade_no, transaction_id, "WeChatPay", total_fee, string.Empty);
                            if (ResultHelper)
                            {
                                //此处可以在添加相关处理业务，校验通知参数中的商户订单号out_trade_no和金额total_fee是否和商户业务系统的单号和金额是否一致，一致后方可更新数据库表中的记录。  
                                WxPayData res = new WxPayData();
                                res.SetValue("return_code", "SUCCESS");
                                res.SetValue("return_msg", "OK");
                                Response.Write(res.ToXml());
                                Response.End();
                            }
                            else
                            {
                                WxPayData res = new WxPayData();
                                res.SetValue("return_code", "FAIL");
                                res.SetValue("return_msg", "FAIL");
                                Response.Write(res.ToXml());
                                Response.End();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Write("微信支付服务端回调错误：" + ex.Message + "\r\n" + ex.StackTrace);
                    WxPayData res = new WxPayData();
                    res.SetValue("return_code", "FAIL");
                    res.SetValue("return_msg", "接收失败");
                    Response.Write(res.ToXml());
                    Response.End();
                }
            }
        }
    }
}